package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.BomMaterial;
import cn.com.ege.mvc.model.business.Material;
import cn.com.ege.mvc.model.business.ModelRule;
import cn.com.ege.mvc.model.business.Shoe;
import cn.com.ege.mvc.model.business.Supplier;
import cn.com.ege.mvc.model.view.Bom;
import cn.com.ege.mvc.service.impl.business.BomMaterialService;
import cn.com.ege.mvc.service.impl.business.MaterialService;
import cn.com.ege.mvc.service.impl.business.ModelRuleService;
import cn.com.ege.mvc.service.impl.business.ShoeService;
import cn.com.ege.mvc.service.impl.business.SupplierService;
import cn.com.ege.mvc.service.impl.view.BomService;
import cn.com.ege.mvc.util.PageController;

public class BomDetailAction extends BaseAction implements IAction,
		Serializable {
	private final static Log logger = LogFactory.getLog(BomAction.class);
	private PageController pager = null;
	private BomService service = null;
	private Bom bom = null;
	private List<Bom> bomList = null;
	private BomMaterialService bomMaterialService = null;
	private BomMaterial bomMaterial = null;
	private MaterialService materialService = null;
	private List<Material> materialList = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		String shoeId = request.getParameter("shoeId");
		request.setAttribute("shoeId", shoeId);
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				bom = new Bom();
				bom.setShoe_id(Long.parseLong(shoeId));
				int totalRows = service.getRecordCount(bom);
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				bomList = service.list(bom, this.pager.getPageStartRow(), pager
						.getPageSize(), "order by id desc", true);
				pager.setData(bomList);

				Shoe shoe = new Shoe();
				shoe.setId(Long.valueOf(shoeId));
				shoe = (new ShoeService()).get(shoe);
				request.setAttribute("shoe", shoe);

				ModelRule mr = new ModelRule();
				mr.setId(shoe.getModel_id());
				mr = (new ModelRuleService()).get(mr);
				request.setAttribute("modelRule", mr);

				return getReturnURL(LIST);
			}
			// 点了添加或点了修改
			else if ("get".equals(act)) {
				bom = service.get(bom);
				materialList = materialService.listAll(new Material());
				request.setAttribute("materialList", materialList);
				List<Supplier> supplierList = (new SupplierService()).listAll(new Supplier());
				request.setAttribute("supplierList", supplierList);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					String datas = request.getParameter("datas");
					String[] data = datas.split(",");
					bomMaterial.setMaterial_id(Long.valueOf(data[0]));
					bomMaterial.setShoe_id(Long.parseLong(shoeId));
					bomMaterialService.save(bomMaterial);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("bom", bom);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}

			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					BomMaterial bm = new BomMaterial();
					if (null != id && !("".equals(id))) {
						bm.setId(Long.parseLong(id));
						bomMaterialService.delete(bm);
					}
				}
				return getReturnURL("getList");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public BomService getService() {
		return service;
	}

	public void setService(BomService service) {
		this.service = service;
	}

	public Bom getBom() {
		return bom;
	}

	public void setBom(Bom bom) {
		this.bom = bom;
	}

	public List<Bom> getBomList() {
		return bomList;
	}

	public void setBomList(List<Bom> bomList) {
		this.bomList = bomList;
	}

	public BomMaterialService getBomMaterialService() {
		return bomMaterialService;
	}

	public void setBomMaterialService(BomMaterialService bomMaterialService) {
		this.bomMaterialService = bomMaterialService;
	}

	public BomMaterial getBomMaterial() {
		return bomMaterial;
	}

	public void setBomMaterial(BomMaterial bomMaterial) {
		this.bomMaterial = bomMaterial;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	public List<Material> getMaterialList() {
		return materialList;
	}

	public void setMaterialList(List<Material> materialList) {
		this.materialList = materialList;
	}

}
